﻿using NLog;
using System;

namespace YougeBookmark.Utilities
{
    public class Logger
    {
        private static readonly Lazy<NLog.ILogger> _lazyLogger = new Lazy<NLog.ILogger>(() =>
            LogManager.GetCurrentClassLogger());

        static Logger()
        {
            // 确保加载nlog.config配置文件
            LogManager.LoadConfiguration("nlog.config");
        }

        /// <summary>
        /// 记录跟踪级别日志
        /// </summary>
        public void Trace(string message, params object[] args)
        {
            _lazyLogger.Value.Trace(message, args);
        }

        /// <summary>
        /// 记录调试级别日志（带异常上下文）
        /// </summary>
        public void Debug(Exception ex, string message, params object[] args)
        {
            _lazyLogger.Value.Debug(ex, message, args);
        }

        /// <summary>
        /// 记录普通信息级别日志
        /// </summary>
        public void Info(string message, params object[] args)
        {
            _lazyLogger.Value.Info(message, args);
        }

        /// <summary>
        /// 记录警告级别日志（带格式化参数）
        /// </summary>
        public void Warn(string message, params object[] args)
        {
            _lazyLogger.Value.Warn(message, args);
        }

        /// <summary>
        /// 记录错误级别日志（带异常堆栈）
        /// </summary>
        public void Error(string message, Exception ex)
        {
            _lazyLogger.Value.Error(ex, message);
        }

        /// <summary>
        /// 记录致命错误级别日志（带自定义模板）
        /// </summary>
        public void Fatal(string template, params object[] values)
        {
            _lazyLogger.Value.Fatal(template, values);
        }

        /// <summary>
        /// 条件日志记录（仅当条件为true时记录）
        /// </summary>
        public void Conditional(bool condition, string message, LogLevel level = null)
        {
            if (level == null)
            {
                level = LogLevel.Info;
            }
            if (condition)
            {
                var logEvent = new LogEventInfo(level, _lazyLogger.Value.Name, message);
                _lazyLogger.Value.Log(typeof(Logger), logEvent);
            }
        }
    }
}